package com.hsd.controller;

import com.hsd.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ Author：lite
 * @ Package：com.hsd.controller
 * @ Project：hsd_calculator
 * @ Description: 计算log(n)
 * @ Date：2025/4/15  23:30
 */

@RestController
@RequestMapping("/calculate")
@Slf4j
public class LognController{

    @PostMapping("/logn")
    public Result logn(@RequestBody Map<String,Double>request){
        //num为真数，n为底数
        Double num = request.get("num");
        Double n = request.get("n");
        //判空
        if(num==null||n==null){
            return Result.error("输入不能为空");
        }
        //log计算的真数必须为正,底数非负且不能为1
        if (num<0){
            return Result.error("真数必须是正数");
        }
        if (n<0||n==1){
            return Result.error("底数非负且不能为1");
        }

        log.info("接收到的数:真数num{},底数n:{}",num,n);
        //计算对数
        double result = Math.log(num)/Math.log(n);

        return Result.success(result);
    }
}
